<?php

namespace Apps\Common\Models;

/**
 * 用户订单Model
 * @author     chenxiaolin
 * @date        2016年1月19日
 * @version     2.0.0
 * @copyright   Copyright 2016 meelier.com
 */
class UserOrder extends \PhalconPlus\Database\Model
{

    /**
     *
     * @var integer
     */
    public $order_id;

    /**
     *
     * @var string
     */
    public $order_long_id;

    /**
     *
     * @var integer
     */
    public $user_id;

    /**
     *
     * @var string
     */
    public $order_mobile;

    /**
     *
     * @var integer
     */
    public $order_type;

    /**
     *
     * @var integer
     */
    public $order_origin;

    /**
     *
     * @var double
     */
    public $order_money;

    /**
     *
     * @var double
     */
    public $order_freight;

    /**
     *
     * @var string
     */
    public $order_remake;

    /**
     *
     * @var integer
     */
    public $order_state;

    /**
     *
     * @var integer
     */
    public $order_pay_type;

    /**
     *
     * @var integer
     */
    public $order_pay_state;

    /**
     *
     * @var string
     */
    public $order_addtime;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->hasMany('order_id', 'Apps\Common\Models\UserOrderGoods', 'order_id', array('alias' => 'UserOrderGoods'));
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'user_order';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return UserOrder[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return UserOrder
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * Independent Column Mapping.
     * Keys are the real names in the table and the values their names in the application
     *
     * @return array
     */
    public function columnMap()
    {
        return array(
            'order_id' => 'order_id',
            'order_long_id' => 'order_long_id',
            'user_id' => 'user_id',
            "order_mobile" => "order_mobile",
            'order_type' => 'order_type',
            'order_origin' => 'order_origin',
            'order_money' => 'order_money',
            'order_freight' => 'order_freight',
            'order_remake' => 'order_remake',
            'order_state' => 'order_state',
            'order_pay_type' => 'order_pay_type',
            'order_pay_state' => 'order_pay_state',
            'order_addtime' => 'order_addtime'
        );
    }

    public function defaultColumns()
    {
        return [
            'order_origin',
            'order_freight',
            'order_remake',
            'order_state',
            'order_pay_state',
            'order_addtime',
        ];
    }

    /**
     * 设置订单状态
     * @param $userId
     * @param $orderId
     * @param $state
     * @return bool
     */
    public static function SetOrderState($userId, $orderId, $state)
    {
        $sql = 'UPDATE user_order SET order_state = :state WHERE order_id = :id AND user_id = :uid';
        $model = new self;
        return $model->getWriteConnection()->execute($sql, ['state' => $state ,'id' => $orderId, 'uid' => $userId]);
    }

}
